<template>
  <div>
    <BasicTable
      @register="registerTable"
      :can-resize="true"
      :bordered="true"
      :striped="true"
      :loading="false"
      size="small"
    >
      <template #toolbar>
        <a-button type="primary" @click="onOpenAddBlackList">新增</a-button>
        <a-button @click="onClickRefreshBlackList">刷新黑名单</a-button>
      </template>

      <template #isvalid="{ record }">
        <Tag :color="record.isvalid ? 'green' : 'red'">{{ record.isvalid ? '是' : '否' }}</Tag>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'ant-design:field-time-outlined',
              label: '设置过期',
              color: 'warning',
              ifShow: record.isvalid,
              popConfirm: {
                title: '是否确认设置过期',
                placement: 'left',
                confirm: onSetTokenExpired.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AddBlackList @register="registerAddBlackListForm" @success="afterSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { Tag } from 'ant-design-vue';
  import { ipBlackListColumns, ipBlackListConditionFormSchema } from './blacklist.data';
  import AddBlackList from './AddBlackList.vue';
  //引入子组件

  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';

  //使用modal弹出窗口
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { queryBlackList, setBlackListExpired, refreshBlackList } from '/@/api/platform/blackList';
  const { createMessage } = useMessage();
  const [registerAddBlackListForm, { openModal: openAddBlackListForm }] = useModal();

  export default defineComponent({
    name: 'IpBlackList', //当前组件名称
    components: { BasicTable, TableAction, Tag, AddBlackList },
    setup() {
      //定义模块的名称，提供给子孙组件获取使用
      const moduleName = ref('IP黑名单管理');

      /**
       * 请求后台查询接口返回分页数据显示
       */
      const [registerTable, { reload }] = useTable({
        title: 'IP黑名单管理',
        api: queryBlackList, //api中指定的为table加载数据对应的查询方法
        columns: ipBlackListColumns, //表头数据
        formConfig: {
          labelWidth: 110, //设置查询表单中label的宽度
          schemas: ipBlackListConditionFormSchema, // form查询表单
          showResetButton: false, //查询条件部分中是否显示重置按钮
        },
        pagination: false,
        useSearchForm: true,
        showTableSetting: true, //是否显示表格设置
        bordered: true,
        showIndexColumn: true,
        indexColumnProps: {
          width: 50,
        },
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });

      /**
       * 打开Api日志详情窗口
       * @param record 行数据
       */
      function onOpenAddBlackList(record: Recordable) {
        //使用props传递数据至子组件，在data参数中指定
        let propsData: QcUnifyTransToChildProps = {
          moduleName: moduleName.value,
          operateType: OperationTypeEnum.ADD,
        };
        openAddBlackListForm(true, propsData);
      }

      /**
       * 设置Token过期
       */
      function onSetTokenExpired(record) {
        setBlackListExpired(record.id).then((res) => {
          onClickRefreshBlackList();
          afterSuccess();
        });
      }

      function onClickRefreshBlackList() {
        refreshBlackList().then((res) => {
          if (!res) {
            createMessage.success('刷新黑名单成功');
          } else {
            createMessage.error(res);
          }
        });
      }

      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }

      return {
        registerTable,
        registerAddBlackListForm,
        onOpenAddBlackList,
        onSetTokenExpired,
        afterSuccess,
        onClickRefreshBlackList,
      };
    },
  });
</script>
